Imports System.Text

Public Class VBDORenderEngine
    Inherits VBAbstractRenderEngine

    Public Sub New(ByVal u As UpdateProgress, ByVal s As UpdateStatus)
        MyBase.New(u, s, GetType(SQL))
    End Sub

    Protected Overrides Function FormatFilename(ByVal tableName As String) As String
        Return tableName
    End Function

    Protected Overrides Function RenderPrefix(ByVal t As Table) As String
        Dim sb As New StringBuilder
        sb = sb.Append("Imports pixolut.BizBlox").Append(vbCrLf).Append(vbCrLf)
        sb = sb.Append("Public Class ").Append(t.TableName).Append(vbCrLf)
        sb = sb.Append("    Inherits DataObject").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderPostfix(ByVal t As Table) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        sb = sb.Append("End Class").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderColumn(ByVal t As Table, ByVal index As Integer) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        Dim col As Column = t(index)
        sb = sb.Append("    Private m_").Append(col.ColumnName).Append(" As ").Append(col.DataType).Append(vbCrLf)
        sb = sb.Append(Me.RenderAttributes(col.Attributes))
        sb = sb.Append("    Public Property ").Append(col.ColumnName).Append("() As ").Append(col.DataType).Append(vbCrLf)
        sb = sb.Append("        Get").Append(vbCrLf)
        sb = sb.Append("            Return m_").Append(col.ColumnName).Append(vbCrLf)
        sb = sb.Append("        End Get").Append(vbCrLf)
        sb = sb.Append("        Set(ByVal Value As ").Append(col.DataType).Append(")").Append(vbCrLf)
        sb = sb.Append("            Me.Touch(Columns." & col.ColumnName & ")").Append(vbCrLf)
        sb = sb.Append("            m_").Append(col.ColumnName).Append(" = Value").Append(vbCrLf)
        sb = sb.Append("        End Set").Append(vbCrLf)
        sb = sb.Append("    End Property").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderForeignColumn(ByVal t As Table, ByVal index As Integer) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Function GetBy").Append(t(index).ColumnName).Append("() As ").Append(t(index).FKTable).Append(vbCrLf)
        sb = sb.Append("        Dim obj As new ").Append(t(index).FKTable).Append("(").Append(t(index).ColumnName).Append(")").Append(vbCrLf)
        sb = sb.Append("        obj.Load()").Append(vbCrLf)
        sb = sb.Append("        Return obj").Append(vbCrLf)
        sb = sb.Append("    End Function").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Function GetBy").Append(t(index).ColumnName).Append("(ByVal ParamArray columns() As [Enum]) As ").Append(t(index).FKTable).Append(vbCrLf)
        sb = sb.Append("        Dim obj As new ").Append(t(index).FKTable).Append("(").Append(t(index).ColumnName).Append(")").Append(vbCrLf)
        sb = sb.Append("        obj.Load(columns)").Append(vbCrLf)
        sb = sb.Append("        Return obj").Append(vbCrLf)
        sb = sb.Append("    End Function").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderElements(ByVal t As Table) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf).Append("    Public Shadows Enum Columns").Append(vbCrLf)
        For j As Integer = 0 To t.ColumnCount - 1
            sb = sb.Append("        ").Append(t(j).ColumnName).Append(vbCrLf)
        Next
        sb = sb.Append("    End Enum").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Overrides Function UidColumn() As [enum]").Append(vbCrLf)
        sb = sb.Append("        Return Columns.").Append(t.PKColumnName).Append(vbCrLf)
        sb = sb.Append("    End Function").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Overrides Function TableName() As String").Append(vbCrLf)
        sb = sb.Append("        Return """).Append(t.TableName).Append("""").Append(vbCrLf)
        sb = sb.Append("    End Function").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Shared Function TableNameStatic() As String").Append(vbCrLf)
        sb = sb.Append("        Return """).Append(t.TableName).Append("""").Append(vbCrLf)
        sb = sb.Append("    End Function").Append(vbCrLf)
        If t.GenerateDefaultConstructor Then
            sb = sb.Append(vbCrLf)
            sb = sb.Append("    Public Sub New()").Append(vbCrLf)
            sb = sb.Append("        MyBase.New()").Append(vbCrLf)
            sb = sb.Append("    End Sub").Append(vbCrLf)
        End If

        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Sub New(ByVal uid As Guid)").Append(vbCrLf)
        sb = sb.Append("        MyBase.New(uid)").Append(vbCrLf)
        sb = sb.Append("    End Sub").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Sub New(ByVal uid As Guid, ByVal load As Boolean)").Append(vbCrLf)
        sb = sb.Append("        MyBase.New(uid, load)").Append(vbCrLf)
        sb = sb.Append("    End Sub").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Sub New(ByRef baseCol As ").Append("DOCollection, ByVal index As Integer)").Append(vbCrLf)
        sb = sb.Append("        MyBase.New(baseCol,index)").Append(vbCrLf)
        sb = sb.Append("    End Sub").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    Public Sub New(ByVal dr As System.Data.DataRow, ByVal selections As DOSelection())").Append(vbCrLf)
        sb = sb.Append("        MyBase.New(dr, selections)").Append(vbCrLf)
        sb = sb.Append("    End Sub").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        Return sb.ToString()
    End Function
End Class
